
package com.iplant.wms.controller.brm;

import com.iplant.base.controller.BaseController;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.wms.service.BRMService;
import com.iplant.wms.service.po.brm.BRMReplenishRule;
import com.iplant.wms.service.po.brm.BRMShelvesProp;
import com.iplant.wms.service.po.brm.BRMShelvesRange;
import com.iplant.wms.service.po.brm.BRMShelvesRule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/Shelves")
public class ShelvesController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(ShelvesController.class);

    @Autowired
    BRMService wBRMService;

    @GetMapping("/PropAll")
    public Object PropAll(HttpServletRequest request) {
        Object wResult = new Object();
        try {

            BMSEmployee wLoginUser = GetSession(request);
            int wRuleID = StringUtils.parseInt(request.getParameter("RuleID"), -1);
            String wName = StringUtils.parseString(request.getParameter("Name"));
            String wCode = StringUtils.parseString(request.getParameter("Code"));
            int wActive = StringUtils.parseInt(request.getParameter("Active"), -1);
            Pagination wPagination = GetPagination(request);
            ServiceResult<List<BRMShelvesProp>> wServiceResult = wBRMService.BRM_QueryShelvesPropList(wLoginUser, wRuleID,
                    wName, wCode,
                    wActive, wPagination);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/PropUpdate")
    public Object PropUpdate(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);
            int wUserID = wBMSEmployee.ID;
            String wUserName = wBMSEmployee.Name;

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            BRMShelvesProp wBRMShelvesProp = CloneTool.Clone(wParam.get("data"), BRMShelvesProp.class);
            wBRMShelvesProp.EditorName = wUserName;
            wBRMShelvesProp.EditorID = wUserID;
            wBRMShelvesProp.EditTime = Calendar.getInstance();
            if (wBRMShelvesProp.ID <= 0) {

                wBRMShelvesProp.CreatorName = wUserName;
                wBRMShelvesProp.CreatorID = wUserID;
                wBRMShelvesProp.CreateTime = Calendar.getInstance();

            }
            ServiceResult<Integer> wServiceResult = wBRMService.BRM_UpdateShelvesProp(wBMSEmployee, wBRMShelvesProp);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wBRMShelvesProp);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }


    @PostMapping("/PropActive")
    public Object PropActive(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            int wActive = wParam.containsKey("Active") ? (int) wParam.get("Active") : 0;

            List<BRMShelvesProp> wBRMShelvesPropList = CloneTool.CloneArray(wParam.get("data"), BRMShelvesProp.class);
            ServiceResult<Integer> wServiceResult = new ServiceResult<Integer>(0);
            for (BRMShelvesProp wBRMShelvesProp : wBRMShelvesPropList) {

                wServiceResult = wBRMService.BRM_ActiveShelvesProp(wBMSEmployee, wBRMShelvesProp, wActive);

                if (!StringUtils.isEmpty(wServiceResult.getFaultCode()))
                    break;
            }

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @GetMapping("/RuleAll")
    Object RuleAll(HttpServletRequest request) {
        Object wResult = new Object();
        try {

            BMSEmployee wLoginUser = GetSession(request);
            String wName = StringUtils.parseString(request.getParameter("Name"));
            int wActive = StringUtils.parseInt(request.getParameter("Active"), -1);
            int wStoreID = StringUtils.parseInt(request.getParameter("StoreID"), -1);
            Pagination wPagination = GetPagination(request);
            ServiceResult<List<BRMShelvesRule>> wServiceResult = wBRMService.BRM_QueryShelvesRuleList(wLoginUser, wName, wStoreID, wActive, wPagination);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @GetMapping("/RuleInfo")
    Object RuleInfo(HttpServletRequest request) {
        Object wResult = new Object();
        try {

            BMSEmployee wLoginUser = GetSession(request);

            int wID = StringUtils.parseInt(request.getParameter("ID"));
            String wCode = StringUtils.parseString(request.getParameter("Code"));

            ServiceResult<BRMShelvesRule> wServiceResult = wBRMService.BRM_QueryShelvesRule(wLoginUser, wID, wCode);
            BRMShelvesRule wServerRst = wServiceResult.Result;

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServerRst);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), null, wServerRst);
            }

        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/RuleUpdate")
    Object RuleUpdate(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);
            int wUserID = wBMSEmployee.ID;
            String wUserName = wBMSEmployee.Name;

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            BRMShelvesRule wBRMShelvesRule = CloneTool.Clone(wParam.get("data"), BRMShelvesRule.class);
            wBRMShelvesRule.EditorName = wUserName;
            wBRMShelvesRule.EditorID = wUserID;
            wBRMShelvesRule.EditTime = Calendar.getInstance();
            if (wBRMShelvesRule.ID <= 0) {

                wBRMShelvesRule.CreatorName = wUserName;
                wBRMShelvesRule.CreatorID = wUserID;
                wBRMShelvesRule.CreateTime = Calendar.getInstance();

            }
            ServiceResult<Integer> wServiceResult = wBRMService.BRM_UpdateShelvesRule(wBMSEmployee, wBRMShelvesRule);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wBRMShelvesRule);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/RuleDelete")
    Object RuleDelete(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            BRMShelvesRule wBRMShelvesRule = CloneTool.Clone(wParam.get("data"), BRMShelvesRule.class);

            ServiceResult<Integer> wServiceResult = wBRMService.BRM_DeleteShelvesRule(wBMSEmployee, wBRMShelvesRule);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/RuleActive")
    Object RuleActive(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            int wActive = wParam.containsKey("Active") ? (int) wParam.get("Active") : 0;

            List<BRMShelvesRule> wBRMShelvesRuleList = CloneTool.CloneArray(wParam.get("data"), BRMShelvesRule.class);
            ServiceResult<Integer> wServiceResult = new ServiceResult<Integer>(0);
            for (BRMShelvesRule wBRMShelvesRule : wBRMShelvesRuleList) {

                wServiceResult = wBRMService.BRM_ActiveShelvesRule(wBMSEmployee, wBRMShelvesRule, wActive);

                if (!StringUtils.isEmpty(wServiceResult.getFaultCode()))
                    break;
            }

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }


    @GetMapping("/RangeAll")
    Object RangeAll(HttpServletRequest request) {
        Object wResult = new Object();
        try {

            BMSEmployee wLoginUser = GetSession(request);
            int wStoreID = StringUtils.parseInt(request.getParameter("StoreID"), -1);
            List<Integer> wCustomerIDList = StringUtils.parseIntList(request.getParameter("CustomerID"), ",");
            int wMaterialGroupID = StringUtils.parseInt(request.getParameter("MaterialGroupID"), -1);
            int wMaterialID = StringUtils.parseInt(request.getParameter("MaterialID"), -1);
            String wMaterialLike = StringUtils.parseString(request.getParameter("MaterialLike"));
            String wMaterialCode = StringUtils.parseString(request.getParameter("MaterialCode"));

            String wMaterialGroupFullName = StringUtils.parseString(request.getParameter("MaterialGroupFullName"));
            Pagination wPagination = GetPagination(request);

            ServiceResult<List<BRMShelvesRange>> wServiceResult = wBRMService.BRM_QueryShelvesRangeList(
                    wLoginUser, wStoreID,  wCustomerIDList, wMaterialGroupID, wMaterialGroupFullName, wMaterialID, wMaterialCode, wMaterialLike, wPagination);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }


    @PostMapping("/RangeUpdate")
    Object RangeUpdate(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            BRMShelvesRange wBRMShelvesRange = CloneTool.Clone(wParam.get("data"), BRMShelvesRange.class);

            wBRMShelvesRange.setUserInfo(wBMSEmployee);

            ServiceResult<Integer> wServiceResult = wBRMService.BRM_UpdateShelvesRange(wBMSEmployee, wBRMShelvesRange);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wBRMShelvesRange);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/RangeDelete")
    Object RangeDelete(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            BRMShelvesRange wBRMShelvesRange = CloneTool.Clone(wParam.get("data"), BRMShelvesRange.class);

            wBRMShelvesRange.setUserInfo(wBMSEmployee);

            ServiceResult<Integer> wServiceResult = wBRMService.BRM_DeleteShelvesRange(wBMSEmployee, wBRMShelvesRange);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wBRMShelvesRange);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }


    @GetMapping("/ReplenishRuleAll")
    Object ReplenishRuleAll(HttpServletRequest request) {
        Object wResult = new Object();
        try {

            BMSEmployee wLoginUser = GetSession(request);
            String wName = StringUtils.parseString(request.getParameter("Name"));
            int wActive = StringUtils.parseInt(request.getParameter("Active"), -1);
            int wShelvesRuleID = StringUtils.parseInt(request.getParameter("ShelvesRuleID"), -1);
            int wStoreID = StringUtils.parseInt(request.getParameter("StoreID"), -1);
            Pagination wPagination = GetPagination(request);
            ServiceResult<List<BRMReplenishRule>> wServiceResult = wBRMService.BRM_QueryReplenishRuleList(wLoginUser,
                    wName, wStoreID,wShelvesRuleID, wActive, wPagination);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @GetMapping("/ReplenishRuleInfo")
    Object ReplenishRuleInfo(HttpServletRequest request) {
        Object wResult = new Object();
        try {

            BMSEmployee wLoginUser = GetSession(request);

            int wID = StringUtils.parseInt(request.getParameter("ID"));
            String wCode = StringUtils.parseString(request.getParameter("Code"));

            ServiceResult<BRMReplenishRule> wServiceResult = wBRMService.BRM_QueryReplenishRule(wLoginUser, wID, wCode);
            BRMReplenishRule wServerRst = wServiceResult.Result;

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServerRst);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), null, wServerRst);
            }

        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/ReplenishRuleUpdate")
    Object ReplenishRuleUpdate(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);
            int wUserID = wBMSEmployee.ID;
            String wUserName = wBMSEmployee.Name;

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            BRMReplenishRule wBRMReplenishRule = CloneTool.Clone(wParam.get("data"), BRMReplenishRule.class);
            wBRMReplenishRule.EditorName = wUserName;
            wBRMReplenishRule.EditorID = wUserID;
            wBRMReplenishRule.EditTime = Calendar.getInstance();
            if (wBRMReplenishRule.ID <= 0) {

                wBRMReplenishRule.CreatorName = wUserName;
                wBRMReplenishRule.CreatorID = wUserID;
                wBRMReplenishRule.CreateTime = Calendar.getInstance();

            }
            ServiceResult<Integer> wServiceResult = wBRMService.BRM_UpdateReplenishRule(wBMSEmployee, wBRMReplenishRule);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wBRMReplenishRule);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/ReplenishRuleDelete")
    Object ReplenishRuleDelete(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            BRMReplenishRule wBRMReplenishRule = CloneTool.Clone(wParam.get("data"), BRMReplenishRule.class);

            ServiceResult<Integer> wServiceResult = wBRMService.BRM_DeleteReplenishRule(wBMSEmployee, wBRMReplenishRule);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/ReplenishRuleActive")
    Object ReplenishRuleActive(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            int wActive = wParam.containsKey("Active") ? (int) wParam.get("Active") : 0;

            List<BRMReplenishRule> wBRMReplenishRuleList = CloneTool.CloneArray(wParam.get("data"), BRMReplenishRule.class);
            ServiceResult<Integer> wServiceResult = new ServiceResult<Integer>(0);
            for (BRMReplenishRule wBRMReplenishRule : wBRMReplenishRuleList) {

                wServiceResult = wBRMService.BRM_ActiveReplenishRule(wBMSEmployee, wBRMReplenishRule, wActive);

                if (!StringUtils.isEmpty(wServiceResult.getFaultCode()))
                    break;
            }

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }


}
